package com.shujia.MR.output;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Unable to initialize MapOutputCollector org.apache.hadoop.mapred.MapTask$MapOutputBuffer
 * java.lang.NullPointerException
 * <p>
 * 遇到该错误说明当前Map端输出类有问题，通常是由于输出类没有序列化导致
 */
public class Student implements WritableComparable<Student>  {
    //1500100001,施笑槐,22,女,文科六班
    String studentID;
    String name;
    String age;
    String gender;
    String clazz;
    String score;

    @Override
    public String toString() {
        return studentID + "\t" + name + "\t" + age + "\t" + gender + "\t" + clazz + "\t" + score;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public Student() {
    }

    public Student(String studentID, String name, String age, String gender, String clazz, String score) {
        this.studentID = studentID;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
        this.score = score;
    }

    /**
     * 序列化过程，将当前属性，转为二进制
     *
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        // UTF表示对应字符串
        out.writeUTF(this.studentID);
        out.writeUTF(this.name);
        out.writeUTF(this.age);
        out.writeUTF(this.gender);
        out.writeUTF(this.clazz);
        out.writeUTF(this.score);
    }

    /**
     * 反序列化过程读取对应属性并赋予给新的对象
     *
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.studentID = in.readUTF();
        this.name = in.readUTF();
        this.age = in.readUTF();
        this.gender = in.readUTF();
        this.clazz = in.readUTF();
        this.score = in.readUTF();
    }

    /**
     * int thisValue = this.value;
     * int thatValue = o.value;
     * return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
     */
    @Override
    public int compareTo(Student other) {
        int thisScore = Integer.valueOf(this.score).intValue();
        int otherScore = Integer.valueOf(other.score).intValue();
        return (thisScore < otherScore ? -1 : (thisScore == otherScore ? 0 : 1));
    }


}
